package com.dream.demoquickstart.service.impl;

import com.dream.demoquickstart.mapper.ArticleCategoryMapper;
import com.dream.demoquickstart.mapper.ArticleMapper;
import com.dream.demoquickstart.mapper.ArticleStateMapper;
import com.dream.demoquickstart.pojo.Article;
import com.dream.demoquickstart.pojo.ArticleCategory;
import com.dream.demoquickstart.pojo.ArticleState;
import com.dream.demoquickstart.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @version 1.0
 * @program: demo-quickstart
 * @className ArticleServiceImpl
 * @description:
 * @author: Administrator
 * @create: 2025-08-19 16:19
 **/
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleCategoryMapper articleCategoryMapper;
    @Autowired
    private ArticleStateMapper articleStateMapper;

    @Override
    public void insertArticle(Article article) {
        // 设置文章的创建时间为当前时间
        article.setTime(LocalDateTime.now().toString());
        articleMapper.insertArticle(article);
    }

    @Override
    public void updateArticle(Article article) {
        // 设置文章的更新时间为当前时间
        article.setTime(LocalDateTime.now().toString());
        articleMapper.updateArticle(article);
    }

    @Override
    public void deleteArticleById(Integer id) {
        articleMapper.deleteArticleById(id);
    }

    @Override
    public Article selectArticleById(Integer id) {
        return articleMapper.selectArticleById(id);
    }

    @Override
    public PageInfo<Article> selectAllArticle(Integer pageNum, Integer pageSize, Integer categoryId, String stateId) {
        // 1. 设置分页参数
        PageHelper.startPage(pageNum, pageSize);

        // 2. 执行查询 (紧跟在startPage后的第一个查询会被分页)
        List<Article> articleList = articleMapper.selectAllArticle(categoryId, stateId);

        // 3. 将查询结果封装到 PageInfo 对象中
        return new PageInfo<>(articleList);
    }

    @Override
    public List<ArticleCategory> selectAllCategory() {
        return articleCategoryMapper.selectAllCategory();
    }

    @Override
    public List<ArticleState> selectAllState() {
        return articleStateMapper.selectAllState();
    }
}
